<script setup lang="ts">
import type { AddressItem } from '@/types/address'
import { getMemberAddressAPI } from '@/services/address'
import { onLoad, onShow } from '@dcloudio/uni-app'
import { ref, onMounted } from 'vue'
import type { RadioGroupOnChangeEvent } from '@uni-helper/uni-app-types'
import { useAddressStore } from '@/stores/modules/address'
//子调父
const emit = defineEmits<{
  (event: 'close'): void
}>()
//获取地址列表
const addressList = ref<AddressItem[]>()
const getMemberAddressData = async () => {
  const res = await getMemberAddressAPI()
  addressList.value = res.result
}

//页面显示的时候调用
onMounted(() => {
  getMemberAddressData()
})
//选择的地址
const selectedAddress = ref<AddressItem>()
const onChangeAddress = (ev: RadioGroupOnChangeEvent) => {
  selectedAddress.value = addressList.value?.find((v) => v.id === ev.detail.value)
  //修改地址
  const addressStore = useAddressStore()
  addressStore.changeSelectedAddress(selectedAddress.value!)
  emit('close')
}
</script>

<template>
  <view class="address-panel">
    <!-- 关闭按钮 -->
    <text class="close icon-close" @tap="emit('close')"></text>
    <!-- 标题 -->
    <view class="title">配送至</view>
    <!-- 内容 -->

    <radio-group @change="onChangeAddress">
      <view class="content" v-for="item in addressList" :key="item.id">
        <view class="item">
          <view class="item-content">
            <view class="user"
              >{{ item.receiver }}
              <text class="contact">{{ item.contact }}</text>
            </view>
            <view class="address">{{ item.fullLocation }}{{ item.address }}</view>
          </view>
          <radio
            color="#27ba9b"
            class="radio1"
            :value="item.id"
            :checked="item.isDefault ? true : false"
          />
        </view>
      </view>
    </radio-group>

    <!-- 添加按钮 -->
    <!-- <view class="add-btn">
      <navigator hover-class="none" url="/pagesMember/address-form/address-form">
        新建地址
      </navigator>
    </view> -->
  </view>
</template>

<style lang="scss">
.address-panel {
  padding: 0 30rpx;
  border-radius: 10rpx 10rpx 0 0;
  position: relative;
  background-color: #fff;
}

.title {
  line-height: 1;
  padding: 40rpx 0;
  text-align: center;
  font-size: 32rpx;
  font-weight: normal;
  border-bottom: 1rpx solid #ddd;
  color: #444;
}

.close {
  position: absolute;
  right: 24rpx;
  top: 24rpx;
}

.content {
  padding: 20rpx;

  .item {
    display: flex;
    justify-content: space-around;
    align-items: center;

    // padding: 10rpx 10rpx 10rpx 60rpx;
    background-size: 40rpx;
    background-repeat: no-repeat;
    background-position: 0 center;
    background-image: url(https://pcapi-xiaotuxian-front-devtest.itheima.net/miniapp/images/locate.png);
    position: relative;
  }
  .item-content {
    display: flex;
    flex: 1;
    padding-left: 60rpx;
    padding-right: 20rpx;
    flex-direction: column;
    .user {
      display: flex;
      align-items: center;
      justify-content: space-between;
      font-size: 30rpx;
      color: #444;

      .contact {
        font-size: 28rpx;
        // padding-left: 20rpx;
        color: #666;
      }
    }
  }
  .radio1 {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 40rpx;
    height: 100%;
  }

  .address {
    display: flex;
    flex: 1;
    line-height: 1.6;
    font-size: 26rpx;
    color: #333;
  }
}

.footer {
  display: flex;
  justify-content: space-between;
  padding: 20rpx 0 40rpx;
  font-size: 28rpx;
  color: #444;

  .button {
    flex: 1;
    height: 72rpx;
    text-align: center;
    line-height: 72rpx;
    margin: 0 20rpx;
    color: #fff;
    border-radius: 72rpx;
  }

  .primary {
    color: #fff;
    background-color: #27ba9b;
  }

  .secondary {
    background-color: #ffa868;
  }
}

.add-btn {
  height: 80rpx;
  text-align: center;
  line-height: 80rpx;
  margin: 30rpx 20rpx;
  color: #fff;
  border-radius: 80rpx;
  font-size: 30rpx;
  background-color: #27ba9b;
}
</style>
